package com.itheima;

import com.itheima.config.DbConfig;
import com.itheima.config.SpringConfig;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.List;


public class UserApp {
    @Test
    public void test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        UserServiceImpl service = context.getBean(UserServiceImpl.class);
        service.destroy();
    }

    @Test
    public void test2() {
        //通过加载spring配置文件来获得ioc容器
//        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        //通过id名称来获得数据库连接池对象
//        DataSource dataSource = (DataSource)context.getBean("druidDataSource");

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        DataSource dataSource = applicationContext.getBean(DataSource.class);
        System.out.println(dataSource);
    }

    @Test
    public void test3(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);

        UserService userService = applicationContext.getBean(UserService.class);

        List<User> userList = userService.findAll();

        for (User user : userList) {
            System.out.println("user = " + user);
        }

    }

}
